package com.falling.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.falling.reggie.common.CustomException;
import com.falling.reggie.entity.Category;
import com.falling.reggie.entity.Dish;
import com.falling.reggie.entity.SetMeal;
import com.falling.reggie.mapper.CategoryMapper;
import com.falling.reggie.service.CategoryService;
import com.falling.reggie.service.DishService;
import com.falling.reggie.service.SetMealService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Resource
    private DishService dishService;

    @Resource
    private SetMealService setMealService;

    @Override
    public boolean remove(Long id) {
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId, id);
        int dishCount = dishService.count(dishQueryWrapper);
        if (dishCount > 0){
            throw new CustomException("this category related Dish...");
        }

        LambdaQueryWrapper<SetMeal> setMealQueryWrapper = new LambdaQueryWrapper<>();
        setMealQueryWrapper.eq(SetMeal::getCategoryId, id);
        int setMealCount = setMealService.count(setMealQueryWrapper);
        if (setMealCount > 0){
            throw new CustomException("this category related SetMeal...");
        }
        return super.removeById(id);
    }
}
